﻿
@{
    ViewData["Title"] = "爬取的文章详情页";
    var art = ViewData["art"] as article;
    var article = ViewData["article"] as article_crawler;
    var cateList = ViewData["cate"] as List<article_category>;
    var slist = cateList.Select(x => new SelectListItem()
    {
        Text = x.is_show ? x.category_name : $"{x.category_name}-不显示",
        Value = x.id.ToString(),
        Selected = article.cateid == x.id
    }).ToList();
}

<div class="row">
    <form id="ajaxForm" enctype="multipart/form-data" class="form-horizontal">
        <input type="hidden" name="id" asp-for="@article.id" />
        <div class="container-fluid">
            <div class="row form-group">
                <label class="control-label col-md-2">标题</label>
                <div class="col-md-10">
                    <input class="form-control" type="text" name="title" id="title" asp-for="@article.title" />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">分类</label>
                <div class="col-md-10">
                    <select class="form-control" asp-items="slist" id="cateid" name="cateid"></select>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">标签</label>
                <div class="col-md-10">
                    <div class="form-control">
                        @{
                            if (!string.IsNullOrWhiteSpace(article.tag))
                            {
                                var tags = JsonConvert.DeserializeObject<List<string>>(article.tag);
                                foreach (var tag in tags)
                                {
                                    <span>@tag </span>
                                }
                            }
                        }
                    </div>
                </div>
            </div>

            <div class="row form-group">
                <label class="control-label col-md-2">内容</label>
                <div class="col-md-10">
                    <div id="editor">
                        @Html.Raw(article.content)
                    </div>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">来源Url</label>
                <div class="col-md-10">
                    <div class="inline">
                        <div class="control-label">
                            <a href="@article.url" target="_blank">@article.url</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">封面图片</label>
                <div class="col-md-6">
                    <input class="form-control" type="text" name="url" asp-for="@article.img" readonly />
                </div>
                <div class="col-md-4">
                    <a href="@article.img" target="_blank"><img src="@article.img" /></a>
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">收录状态</label>
                <div class="col-md-10">
                    <input class="form-control" name="is_save" id="is_save" type="text" value="@(article.is_save ? "已收录" : "")" readonly />
                </div>
            </div>

            <div class="row form-group">
                <label class="control-label col-md-2">发布时间</label>
                <div class="col-md-10">
                    <input class="form-control" name="publish_date" id="publish_date" type="text" value="@article.publish_date.ToLocalDateTimeString()" readonly />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">创建时间</label>
                <div class="col-md-10">
                    <input class="form-control" name="create_time" id="create_time" type="text" value="@article.create_time.ToLocalDateTimeString()" readonly />
                </div>
            </div>
            <div class="row form-group">
                <label class="control-label col-md-2">更新时间</label>
                <div class="col-md-10">
                    <input class="form-control" name="last_update" id="last_update" type="text" value="@article.last_update.ToLocalDateTimeString()" readonly />
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6 col-md-offset-2">
                    @if (!article.is_save)
                    {
                        <button type="button" class="btn btn-primary" onclick="SaveArticleCrawler()">保存</button>
                        <button type="button" class="btn btn-primary" onclick="SaveArticle()">收录</button>
                    }
                    @if (art != null)
                    {
                        <a href="@Url.Action("Detail","Article",new { id = art.id })" target="_blank">查看关联文章</a>
                    }
                </div>
            </div>
        </div>
    </form>
</div>

@section scripts{

    <script type="text/javascript">
        var E = window.wangEditor
        var editor = new E('#editor')
        // 或者 var editor = new E( document.getElementById('editor') )
        // editor.customConfig.uploadImgShowBase64 = true
        // 将图片大小限制为 3M
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
        editor.customConfig.uploadImgServer = '/upload/image'
        // 限制一次最多上传 5 张图片
        editor.customConfig.uploadImgMaxLength = 5
        editor.customConfig.uploadFileName = 'image'
        editor.create();

        E.fullscreen.init(editor);
        E.viewSource.init(editor);

        function SaveArticleCrawler() {
            if ($("#title").val().length === 0) {
                utils.ShowError("请输入标题");
                return;
            }
            if (editor.txt.text().length === 0) {
                utils.ShowError("请输入文章内容");
                return;
            }

            $("#ajaxForm").ajaxSubmit({
                type: 'post',
                url: '@Url.Action("Update")',
                data: {
                    content: editor.txt.html()
                },  // data是作为额外提交的表单数据
                success: function (e) {
                    if (e.msg == 'ok') {
                        utils.ShowSuccess(e.data, function () {
                            location.reload(true);
                        });
                    }
                },
                error: function (e) {
                    utils.ShowError(e.data);
                }
            });
        }

        function SaveArticle() {
            $("#ajaxForm").ajaxSubmit({
                type: 'post',
                url: '@Url.Action("SaveArticle")',
                data: {

                },  // data是作为额外提交的表单数据
                success: function (e) {
                    if (e.msg == 'ok') {
                        utils.ShowSuccess(e.data, function () {
                            location.reload(true);
                        });
                    } else {
                        utils.ShowError(e.data);
                    }
                },
                error: function (e) {
                    utils.ShowError(e.data);
                }
            });
        }
    </script>
}